package com.superatom.springboot.service;

import com.superatom.springboot.domain.TUsers;
import com.superatom.springboot.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class UsersService {

    @Autowired
    private UsersRepository usersRepository;

    @Transactional
    public void saveUserTwo(){
        TUsers usersA = new TUsers();
        usersA.setName("Lucy");
        usersA.setPhoneNum("1");
        usersA.setAge(22);
        usersRepository.save(usersA);

        TUsers usersB = new TUsers();
        usersB.setName("Cat");
        usersB.setPhoneNum("666666");
        usersB.setAge(21);
        usersRepository.save(usersB);
    }

    public void getAge(Integer id) throws Exception{
        TUsers users = usersRepository.findById(id).get();
        Integer age = users.getAge();

        if (age < 12) {
            throw new Exception("你是不是读小学");
        } else if (age >= 12 && age < 18) {
            throw new Exception("你是不是读初中");
        }

    }
}
